package com.hxzy.controller;

import cn.hutool.jwt.JWTUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hxzy.util.WeixinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/weixin")
public class WexinController {

    @Autowired
    private WeixinUtil weixinUtil;

    @PostMapping(value = "/login")
    public String  login(@RequestBody Map<String,Object> map) throws JsonProcessingException {

       String openId=this.weixinUtil.getOpenId(map.get("code").toString());

       //去数据库查询   openId

        boolean openIdExists=false;

        if(!openIdExists){
            //注册用户信息  insert
            /*
             avatarUrl:avatarUrl, city:city,  country:country,  gender:gender,  language:language,  nickName:nickName,  province:province
             */
        }


        Map<String, Object> dataMap = new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("openId", openId);
            }
        };

        return JWTUtil.createToken(map, "1234".getBytes());
    }

}
